package com.bluedot.www.core.common;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 阿里云 OSS 配置信息，使用单例模式保证只有一个 OSSConfigure 对象
 * @author jiangnan
 * @version 1.0
 * @date 2021/8/16 20:20
 */
public class OSSConfigure {

    private static OSSConfigure ossConfigure = null;
    private String endpoint;
    private String accessKeyId;
    private String accessKeySecret;
    private String bucketName;
    private String accessUrl;
    /**配置文件默认地址*/
    private static final String DEFAULT_LOCATION = "application.properties";


    /**
     * 通过配置文件.properties文件获取，这几项内容。
     *
     * @param storageConfName
     * @throws IOException
     */
    private OSSConfigure(String storageConfName) {

        Properties prop = new Properties();
        InputStream is = super.getClass().getClassLoader().getResourceAsStream(storageConfName);
        try {
            prop.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }

        endpoint = prop.getProperty("OSS.Endpoint").trim();
        accessKeyId = prop.getProperty("OSS.AccessKey").trim();
        accessKeySecret = prop.getProperty("OSS.AccessKeySecret").trim();
        bucketName = prop.getProperty("OSS.BucketName").trim();
        accessUrl = prop.getProperty("OSS.accessUrl").trim();

    }

    /**
     * 获取 OSSConfigure 单例对象
     * @return com.bluedot.www.core.common.OSSConfigure
     * @author He Peng
     * @date 2021/10/19 19:21
     */
    public static OSSConfigure getInstance(String propertyLocation) {
        if (ossConfigure == null) {
            ossConfigure = new OSSConfigure(propertyLocation);
        }
        return ossConfigure;
    }

    public static OSSConfigure getInstance() {
        return getInstance(DEFAULT_LOCATION);
    }


    public String getEndpoint() {
        return endpoint;
    }

    public void setEndpoint(String endpoint) {
        this.endpoint = endpoint;
    }

    public String getAccessKeyId() {
        return accessKeyId;
    }

    public void setAccessKeyId(String accessKeyId) {
        this.accessKeyId = accessKeyId;
    }

    public String getAccessKeySecret() {
        return accessKeySecret;
    }

    public void setAccessKeySecret(String accessKeySecret) {
        this.accessKeySecret = accessKeySecret;
    }

    public String getBucketName() {
        return bucketName;
    }

    public void setBucketName(String bucketName) {
        this.bucketName = bucketName;
    }

    public String getAccessUrl() {
        return accessUrl;
    }

    public void setAccessUrl(String accessUrl) {
        this.accessUrl = accessUrl;
    }

}
